home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / time.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  15KB  |  339 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 09/27/96 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   TimeAdjust() is called to adjust timing variables after each program move  *
  11. *   is made.  it simply increments the number of moves made, decrements the    *
  12. *   amount of time used, and then makes any necessary adjustments based on the *
  13. *   time controls.                                                             *
  14. *   next search.                                                               *
  15. *                                                                              *
  16. ********************************************************************************
  17. */
  18. void TimeAdjust(int time_used, PLAYER player)
  19. {
  20. /*
  21.  ----------------------------------------------------------
  22. |                                                          |
  23. |   decrement the number of moves remaining to the next    |
  24. |   time control.  then subtract the time the program took |
  25. |   to choose its move from the time remaining.            |
  26. |                                                          |
  27.  ----------------------------------------------------------
  28. */
  29.   
  30.   if (player == crafty) {
  31.     tc_moves_remaining--;
  32.     tc_time_remaining-=(tc_time_remaining > time_used) ? 
  33.                        time_used : tc_time_remaining;
  34.     if (!tc_moves_remaining) {
  35.       if (tc_sudden_death == 2) tc_sudden_death=1;
  36.       tc_moves_remaining+=tc_secondary_moves;
  37.       tc_time_remaining+=tc_secondary_time;
  38.       tc_time_remaining_opponent+=tc_secondary_time;
  39.       Print(4095,"time control reached\n");
  40.     }
  41.     if (tc_increment) tc_time_remaining+=tc_increment;
  42.   }
  43.   else {
  44.     tc_time_remaining_opponent-=(tc_time_remaining_opponent > time_used) ? 
  45.                                 time_used : tc_time_remaining_opponent;
  46.     if (tc_increment) tc_time_remaining_opponent+=tc_increment;
  47.   }
  48. }
  49.  
  50. /* last modified 06/15/98 */
  51. /*
  52. ********************************************************************************
  53. *                                                                              *
  54. *   TimeCheck() is used to determine when the search should stop.  it uses     *
  55. *   several conditions to make this determination:  (1) the search time has    *
  56. *   exceeded the time per move limit;  (2) the value at the root of the tree   *
  57. *   has not dropped to low.  (3) if the root move was flagged as "easy" and    *
  58. *   no move has replaced it as best, the search can actually be stopped early  *
  59. *   to save some time on the clock.  if (2) is true, then the time is extended *
  60. *   based on how far the root value has dropped in an effort to avoid whatever *
  61. *   is being lost.                                                             *
  62. *                                                                              *
  63. ********************************************************************************
  64. */
  65. int TimeCheck(int abort)
  66. {
  67.   int time_used;
  68.   int value, last_value, searched;
  69.   int *i, ndone;
  70.   TREE *tree=local[0];
  71. /*
  72.  ----------------------------------------------------------
  73. |                                                          |
  74. |   first, check to see if we are searching the first move |
  75. |   at this depth.  if so, and we run out of time, we can  |
  76. |   abort the search rather than waiting to complete this  |
  77. |   ply=1 move to see if it's better.                      |
  78. |                                                          |
  79.  ----------------------------------------------------------
  80. */
  81.   if (tree->last[0]==(tree->last[1]-1) && !booking && !annotate_mode &&
  82.       !pondering && iteration_depth>4) return(1);
  83.   ndone=0;
  84.   for (i=tree->last[0];i<tree->last[1];i++)
  85.     if (tree->searched_this_root_move[i-tree->last[0]]) ndone++;
  86.   if (ndone == 1) abort=1;
  87.   if (iteration_depth <= 2) return(0);
  88. /*
  89.  ----------------------------------------------------------
  90. |                                                          |
  91. |   now, check to see if we need to "burp" the time to     |
  92. |   let the operator know the search is progressing and    |
  93. |   how much time has been used so far.                    |
  94. |                                                          |
  95.  ----------------------------------------------------------
  96. */
  97.   time_used=(ReadClock(time_type)-start_time);
  98.   if (tree->nodes_searched>noise_level && (display_options&32) && time_used>burp) {
  99. #if defined(SMP)
  100.     Lock(lock_io);
  101. #endif
  102. #if defined(MACOS)
  103.     printf("               %2i   %s\n",iteration_depth,DisplayTime(time_used));
  104. #else
  105.     printf("               %2i   %s\r",iteration_depth,DisplayTime(time_used));
  106. #endif
  107.     burp=(time_used/1500)*1500+1500;
  108.     fflush(stdout);
  109. #if defined(SMP)
  110.     UnLock(lock_io);
  111. #endif
  112.   }
  113.   if (pondering || analyze_mode) return(0);
  114.   if (time_used > absolute_time_limit) return(1);
  115.   if (easy_move && !search_time_limit) {
  116.     if (time_limit>100 && time_used<time_limit/3) return (0);
  117.   }
  118.   else if (time_used < time_limit) return(0);
  119.   if (search_time_limit) return(1);
  120. /*
  121.  ----------------------------------------------------------
  122. |                                                          |
  123. |   ok.  we have used "time_limit" at this point.  now the |
  124. |   question is, can we stop the search?                   |
  125. |                                                          |
  126. |   first, make sure that we have actually found a score   |
  127. |   at the root of the tree.  if not, we can safely stop   |
  128. |   searching without wasting any more time.               |
  129. |                                                          |
  130.  ----------------------------------------------------------
  131. */
  132.   if ((root_value == root_alpha) && !search_failed_low) {
  133.     searched=0;
  134.     for (i=tree->last[0];i<tree->last[1];i++)
  135.       if (tree->searched_this_root_move[i-tree->last[0]]) searched++;
  136.     if (searched == 1) return(1);
  137.   }
  138. /*
  139.  ----------------------------------------------------------
  140. |                                                          |
  141. |   we have a score at the root of the tree, if the        |
  142. |   evaluation is not significantly worse than the last    |
  143. |   evaluation (from the previous iteration...) then we    |
  144. |   safely stop the search.  note also that if the current |
  145. |   evaluation is quite a bit worse, but we are still way  |
  146. |   ahead, we can still avoid using extra time.            |
  147. |                                                          |
  148.  ----------------------------------------------------------
  149. */
  150.   value=root_value;
  151.   last_value=last_search_value;
  152.   if ((value>=last_value-33 && !search_failed_low) ||
  153.       (value>350 && value >= last_value-67)) {
  154.     if (time_used > time_limit*2) return(1);
  155.     else return(abort);
  156.   }
  157. /*
  158.  ----------------------------------------------------------
  159. |                                                          |
  160. |   we are in trouble at the root.  depending on how much  |
  161. |   the score has dropped, increase the search time limit  |
  162. |   to try and correct the problem.  for a positional drop |
  163. |   we can double the search time (this is for a serious   |
  164. |   drop, of course).                                      |
  165. |                                                          |
  166.  ----------------------------------------------------------
  167. */
  168.   if (time_used < time_limit*2.5 && time_used+500<tc_time_remaining) return(0);
  169.   if ((value >= last_value-67 && !search_failed_low) ||
  170.       value>550) return(abort);
  171. /*
  172.  ----------------------------------------------------------
  173. |                                                          |
  174. |   we are in really serious trouble at the root, losing   |
  175. |   material.  increase the time limit to 6X the original  |
  176. |   target, as losing material is tantamount to losing the |
  177. |   game anyway.                                           |
  178. |                                                          |
  179.  ----------------------------------------------------------
  180. */
  181.   if (time_used < time_limit*6 && time_used+500<tc_time_remaining) return(0);
  182.   return(1);
  183. }
  184.  
  185. /* last modified 10/17/97 */
  186. /*
  187. **********